package com.freemalll.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freemalll.common.core.utils.StringUtils;
import com.freemalll.common.core.utils.poi.ExcelUtil;
import com.freemalll.common.core.web.controller.BaseController;
import com.freemalll.common.core.web.domain.AjaxResult;
import com.freemalll.common.core.web.page.TableDataInfo;
import com.freemalll.common.log.annotation.Log;
import com.freemalll.common.log.enums.BusinessType;
import com.freemalll.common.security.annotation.RequiresPermissions;
import com.freemalll.common.security.utils.SecurityUtils;
import com.freemalll.system.domain.SysOrganization;
import com.freemalll.system.service.ISysOrganizationService;

/**
 * 组织机构 信息操作处理
 * 
 * @author freemalll
 */
@RestController
@RequestMapping("/organization")
public class SysOrganizationController extends BaseController
{
    @Autowired
    private ISysOrganizationService organizationService;

    /**
     * 获取组织机构列表
     */
    @RequiresPermissions("system:organization:list")
    @GetMapping("/list")
    public TableDataInfo list(SysOrganization organization)
    {
        startPage();
        List<SysOrganization> list = organizationService.selectOrganizationList(organization);
        return getDataTable(list);
    }

    @Log(title = "组织机构", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:organization:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOrganization organization)
    {
        List<SysOrganization> list = organizationService.selectOrganizationList(organization);
        ExcelUtil<SysOrganization> util = new ExcelUtil<SysOrganization>(SysOrganization.class);
        util.exportExcel(response, list, "组织机构数据");
    }

    /**
     * 根据组织机构编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        return success(organizationService.selectOrganizationById(id));
    }

    /**
     * 根据组织名称查询组织机构信息
     */
    @GetMapping(value = "/orgName/{orgName}")
    public AjaxResult getInfoByOrgName(@PathVariable String orgName)
    {
        return success(organizationService.selectOrganizationByOrgName(orgName));
    }

    /**
     * 根据组织唯一编码查询组织机构信息
     */
    @GetMapping(value = "/orgOnlyCode/{orgOnlyCode}")
    public AjaxResult getInfoByOrgOnlyCode(@PathVariable String orgOnlyCode)
    {
        return success(organizationService.selectOrganizationByOrgOnlyCode(orgOnlyCode));
    }

    /**
     * 根据父节点ID查询子组织机构
     */
    @GetMapping(value = "/parent/{parentId}")
    public AjaxResult getInfoByParentId(@PathVariable String parentId)
    {
        return success(organizationService.selectOrganizationByParentId(parentId));
    }

    /**
     * 新增组织机构
     */
    @RequiresPermissions("system:organization:add")
    @Log(title = "组织机构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysOrganization organization)
    {
        if (!organizationService.checkOrgNameUnique(organization))
        {
            return error("新增组织'" + organization.getOrgName() + "'失败，组织名称已存在");
        }
        if (StringUtils.isNotEmpty(organization.getOrgOnlyCode()) && !organizationService.checkOrgOnlyCodeUnique(organization))
        {
            return error("新增组织'" + organization.getOrgName() + "'失败，组织唯一编码已存在");
        }
        organization.setCreateName(SecurityUtils.getUsername());
        return toAjax(organizationService.insertOrganization(organization));
    }

    /**
     * 修改组织机构
     */
    @RequiresPermissions("system:organization:edit")
    @Log(title = "组织机构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysOrganization organization)
    {
        if (!organizationService.checkOrgNameUnique(organization))
        {
            return error("修改组织'" + organization.getOrgName() + "'失败，组织名称已存在");
        }
        if (StringUtils.isNotEmpty(organization.getOrgOnlyCode()) && !organizationService.checkOrgOnlyCodeUnique(organization))
        {
            return error("修改组织'" + organization.getOrgName() + "'失败，组织唯一编码已存在");
        }
        organization.setUpdateName(SecurityUtils.getUsername());
        return toAjax(organizationService.updateOrganization(organization));
    }

    /**
     * 删除组织机构
     */
    @RequiresPermissions("system:organization:remove")
    @Log(title = "组织机构", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        organizationService.deleteOrganizationByIds(ids);
        return success();
    }

    /**
     * 获取组织树选择列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysOrganization organization)
    {
        List<SysOrganization> organizations = organizationService.selectOrganizationList(organization);
        return success(organizationService.buildOrganizationTreeSelect(organizations));
    }
}